/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.iFrames.HetmapFrameComponents;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.Point2D;
import java.io.File;
import java.text.DecimalFormat;

import javax.swing.JPopupMenu;

import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.annotations.XYAnnotation;
import org.jfree.chart.annotations.XYBoxAnnotation;
import org.jfree.chart.annotations.XYImageAnnotation;
import org.jfree.chart.annotations.XYLineAnnotation;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.LegendItemEntity;
import org.jfree.chart.entity.XYItemEntity;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.Range;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.TextAnchor;

import toolz.PolarGlyphBuilder;
import toolz.netCdfTools;
import DATA.iDataSet;
import DATA.iDataSetParts.Measurement;
import DATA.iDataSetParts.Peak;
import DATA.iDataSetParts.Peaklist;
import DATA.iDataSetParts.SubstanceInfo;
import GUI.ChartTools.legendItemEditorPopup;

/**
 * 
 * @author Bunkowski
 */
public class HeatmapMouseActions implements ChartMouseListener, MouseListener, MouseMotionListener, MouseWheelListener {

	private HeatmapInternalFrame hif;
	private XYPlot heatmapPlot;
	private Measurement measurement;
	private iDataSet idataset;
	private ChartPanel cp;
	private double[][] rawDataMapAligned;

	public HeatmapMouseActions(HeatmapInternalFrame hif) {
		this.hif = hif;
		heatmapPlot = hif.heatmapPlot;
		measurement = hif.measurement;
		idataset = hif.idataset;
		cp = hif.cp;
		rawDataMapAligned = hif.rawDataMapAligned;
	}

	public void chartMouseClicked(final ChartMouseEvent arg0) {
		MouseEvent trigger = arg0.getTrigger();
		ChartEntity ce = arg0.getEntity();
		Point2D p = getXYonChart(arg0.getTrigger());

		if (ce instanceof LegendItemEntity) {
			LegendItemEntity l = (LegendItemEntity) ce;
			l.getSeriesKey();
			XYSeriesCollection col = ((XYSeriesCollection) hif.spectraplot.getDataset());
			XYSeries s = col.getSeries(l.getSeriesKey());
			JPopupMenu pop = new legendItemEditorPopup(col, s);
			pop.show(cp, arg0.getTrigger().getX(), arg0.getTrigger().getY());
		}

		double xViewSize = heatmapPlot.getDomainAxis().getUpperBound() - heatmapPlot.getDomainAxis().getLowerBound();

		double yViewSize = heatmapPlot.getRangeAxis().getUpperBound() - heatmapPlot.getRangeAxis().getLowerBound();

		double xtol = xViewSize / 40 * hif.visibleRectRatio;
		double ytol = yViewSize / 10 * (2 - hif.visibleRectRatio);

		if (hif.addPeak.isSelected() && ce instanceof XYItemEntity) {

			if (hif.idataset.peaklists.containsKey(measurement.measurementID) == false) {
				String filename = new File(measurement.path).getName().replace("ims.csv", "iphex.pl");
				String[] header = new String[] { "intensity_max/ripmax" + "1/k0" + "RT" + "1/k0_radius" + "RT_radius" };
				Peaklist pl = new Peaklist(header, measurement.measurementID, filename);
				idataset.peaklists.put(measurement.measurementID, pl);
			}

			XYItemEntity xi = (XYItemEntity) ce;
			int item = xi.getItem();

			double x = Math.rint(rawDataMapAligned[0][item] * 1000) / 1000.0;
			double y = Math.rint(rawDataMapAligned[1][item] * 10) / 10.0;
			double intensity = Math.rint(rawDataMapAligned[2][item] * 10000) / 10000.0;

			// y = y/measurement.align ? ;

			Peak klickedPeak = new Peak(new String[] { intensity + "", x + "", y + "", 0.01 + "", 8 + "" });
			idataset.peaklists.get(measurement.measurementID).getPeaks().add(klickedPeak);
			hif.dsif.updateTableWithoutHeatmap();
			hif.showPeaklist();
		}

		if (hif.deletePeak.isSelected()) {

			double xt = p.getX();
			double yt = p.getY();

			if (idataset.peaklists.containsKey(measurement.measurementID)) {
				Peaklist peaklist = idataset.peaklists.get(measurement.measurementID);
				for (int i = 0; i < peaklist.getPeaks().size(); i++) {
					if (peaklist.getPeaks().get(i).rt >= yt - ytol && peaklist.getPeaks().get(i).rt <= yt + ytol) {
						if (peaklist.getPeaks().get(i).dt >= xt - xtol && peaklist.getPeaks().get(i).dt <= xt + xtol) {
							peaklist.getPeaks().remove(i);
							i--;
						}
					}
				}
				hif.showPeaklist();
				hif.dsif.updateTableWithoutHeatmap();
			}
		}

		if (hif.addSubstance.isSelected()) {
			double xt = p.getX();
			double yt = p.getY();

			xt = Math.rint(xt * 10000) / 10000.0;
			yt = Math.rint(yt * 10) / 10.0;

			String substanceName = "Area (" + xt + "-" + yt + ")";

			if (hif.substanceEditingProcess) {

				substanceName = hif.mouseOveredSubstance.name;

			}

			String[] substanceParameter = new String[] { substanceName, xt + "", yt + "",
					Math.rint(xtol * 10000) / 10000.0 + "", Math.rint(ytol * 10) / 10.0 + "" };
			SubstanceInfo si = new SubstanceInfo(substanceParameter);
			idataset.addSubstanceInfo(si);
			hif.dsif.updateTableWithoutHeatmap();

			if (hif.substanceEditingProcess) {
				hif.substanceEditingProcess = false;
				hif.mouseOveredSubstance = null;
				hif.moveArea.setSelected(true);

			}

			hif.showSubstanceInfos();
		}
		if (hif.mouseOveredSubstance != null) {
			idataset.substanceInfos.remove(hif.mouseOveredSubstance);
			hif.showSubstanceInfos();
			hif.substanceEditingProcess = true;
			if (hif.mouseOveredSubstance != null) {
				hif.addSubstance.setSelected(true);
			}
		}

		if (hif.deleteSubstance.isSelected()) {
			double xt = p.getX();
			double yt = p.getY();

			xt = Math.rint(xt * 10000) / 10000.0;
			yt = Math.rint(yt * 10) / 10.0;

			boolean areaDeleted = false;
			for (int i = 0; i < idataset.substanceInfos.size(); i++) {
				if (idataset.substanceInfos.elementAt(i).rt >= yt - ytol
						&& idataset.substanceInfos.elementAt(i).rt <= yt + ytol) {
					if (idataset.substanceInfos.elementAt(i).dt >= xt - xtol
							&& idataset.substanceInfos.elementAt(i).dt <= xt + xtol) {
						idataset.substanceInfos.remove(idataset.substanceInfos.elementAt(i));
						i--;
						areaDeleted = true;
					}
				}
				if (areaDeleted) {
					hif.dsif.updateTableWithoutHeatmap();
					hif.showSubstanceInfos();
					areaDeleted = false;
				}
			}
		}

		if (trigger.isControlDown() && trigger.getButton() == 1 && ce instanceof XYItemEntity && hif.showGcms == false) {
			hif.showSpectraOnMouseover = false;

			XYItemEntity xit = (XYItemEntity) ce;
			int itemt = xit.getItem();
			double yt = rawDataMapAligned[1][itemt];
			// double xt = rawDataMapAligned[0][itemt];

			XYSeries s = new XYSeries("Spectra " + yt);
			s.setDescription(measurement.filename.replace("_ims.csv", ""));
			for (int i = 0; i < hif.scaleK0.length; i++) {
				s.add(hif.scaleK0[i], 0.0);
			}

			for (int i = 0; i < rawDataMapAligned[0].length; i++) {
				if (yt == rawDataMapAligned[1][i]) {
					s.remove(rawDataMapAligned[0][i]);
					s.add(rawDataMapAligned[0][i], rawDataMapAligned[2][i]);
				}
			}
			try {
				hif.klickedpectraDds.getSeries(s.getKey());
			} catch (Exception x) {
				hif.klickedpectraDds.addSeries(s);
				XYLineAnnotation xyla = new XYLineAnnotation(-1, yt, 3, yt);
				heatmapPlot.addAnnotation(xyla);
				hif.klickedSpectraAnnotations.add(xyla);
			}
			hif.spectraplot.setDataset(hif.klickedpectraDds);

			try {
				XYLineAnnotation toAdd = (XYLineAnnotation) hif.spectraDtPositionAnnotation.clone();
				hif.spectraplot.addAnnotation(toAdd);
				XYTextAnnotation toAdd2 = (XYTextAnnotation) hif.spectraPlotCoordinateText.clone();
				hif.spectraplot.addAnnotation(toAdd2);
			} catch (CloneNotSupportedException ex) {
			}

		}

		if (trigger.isControlDown() && trigger.getButton() == 1 && hif.showGcms == true) {

			double yt = p.getY();

			double xMin = hif.gcmsPlot.getDomainAxis().getLowerBound();
			double xMax = hif.gcmsPlot.getDomainAxis().getUpperBound();

			double yMin = hif.gcmsPlot.getRangeAxis().getLowerBound();
			double yMax = hif.gcmsPlot.getRangeAxis().getUpperBound();
			double yTextMargin = (yMax - yMin) / 80;

			hif.gcmsPositionAnno = new XYLineAnnotation(xMin, yt, xMax, yt);
			hif.gcmsPlot.addAnnotation(hif.gcmsPositionAnno);
			double yGcms = netCdfTools.MccToGc(yt);
			DecimalFormat df = new DecimalFormat("#.0");
			hif.gcmsPlotCoordinateText = new XYTextAnnotation(df.format(yGcms), xMax / 3, yt + yTextMargin);
			hif.gcmsPlot.addAnnotation(hif.gcmsPlotCoordinateText);

			hif.klickedGcmsAnnotations.add(hif.gcmsPositionAnno);
			hif.klickedGcmsTextAnnotations.add(hif.gcmsPlotCoordinateText);

			XYLineAnnotation xyla = new XYLineAnnotation(-1, yt, 3, yt);
			heatmapPlot.addAnnotation(xyla);
			hif.klickedSpectraAnnotations.add(xyla);

		}

		// set spectraplot axis to autoscale an remove all marked annotations
		if (trigger.isControlDown() && trigger.getClickCount() == 2) {
			hif.showSpectraOnMouseover = true;
			hif.klickedpectraDds.removeAllSeries();
			for (Object xya : hif.spectraplot.getAnnotations()) {
				hif.spectraplot.removeAnnotation((XYAnnotation) xya);
			}
			for (XYLineAnnotation xyla : hif.klickedSpectraAnnotations) {
				try {
					heatmapPlot.removeAnnotation(xyla);
				} catch (Exception x) {
				}
			}
			for (XYLineAnnotation xyla : hif.klickedGcmsAnnotations) {
				try {
					hif.gcmsPlot.removeAnnotation(xyla);
				} catch (Exception x) {
				}
			}
			for (XYTextAnnotation xyta : hif.klickedGcmsTextAnnotations) {
				try {
					hif.gcmsPlot.removeAnnotation(xyta);
				} catch (Exception x) {
				}
			}
		}

		// klicking the mouswheel aktivates autosclae on subplots
		if (trigger.getButton() == 2) {
			if (trigger.isShiftDown() || trigger.isControlDown()) {
				if (hif.showGcms) {
					hif.gcmsPlot.getDomainAxis().setAutoRange(true);
				} else {
					hif.spectraplot.getRangeAxis().setAutoRange(true);
				}
			}
		}

		hif.showSubstanceInfos();
	}

//	private void setAxisViewTo(Point2D.Double p, double xViewSize, double yViewSize) {
//		heatmapPlot.getDomainAxis().setUpperBound(p.getX() + xViewSize);
//		heatmapPlot.getDomainAxis().setLowerBound(p.getX() - xViewSize);
//		heatmapPlot.getRangeAxis().setUpperBound(p.getY() + yViewSize);
//		heatmapPlot.getRangeAxis().setLowerBound(p.getY() - yViewSize);
//	}
//
//	private void setAxisViewTo(Point2D.Double p) {
//		double xViewSize = heatmapPlot.getDomainAxis().getUpperBound() - heatmapPlot.getDomainAxis().getLowerBound();
//		double yViewSize = heatmapPlot.getRangeAxis().getUpperBound() - heatmapPlot.getRangeAxis().getLowerBound();
//
//		heatmapPlot.getDomainAxis().setUpperBound(p.getX() + xViewSize / 2);
//		heatmapPlot.getDomainAxis().setLowerBound(p.getX() - xViewSize / 2);
//		heatmapPlot.getRangeAxis().setUpperBound(p.getY() + yViewSize / 2);
//		heatmapPlot.getRangeAxis().setLowerBound(p.getY() - yViewSize / 2);
//	}

	private Point2D.Double getXYonChart(MouseEvent e) {
		XYPlot xyp = (XYPlot) heatmapPlot;
		Point2D p = cp.translateScreenToJava2D(e.getPoint());
		PlotOrientation orientation = xyp.getOrientation();
		RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(xyp.getDomainAxisLocation(), orientation);
		RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(xyp.getRangeAxisLocation(), orientation);

		ValueAxis domain = xyp.getDomainAxis();
		ValueAxis range = xyp.getRangeAxis();
		ChartRenderingInfo cri = cp.getChartRenderingInfo();
		int heatmapSubPlot = 0;
		if (hif.showGcms) {
			heatmapSubPlot = 1;
		}
		if (hif.showHeatmapOnly) {
			heatmapSubPlot = 0;
		}
		PlotRenderingInfo pri = cri.getPlotInfo().getSubplotInfo(heatmapSubPlot);
		double x = domain.java2DToValue(p.getX(), pri.getDataArea(), domainEdge);
		double y = range.java2DToValue(p.getY(), pri.getDataArea(), rangeEdge);

		x = Math.rint(x * 10000) / 10000.0;
		y = Math.rint(y * 100) / 100.0;
		return new Point2D.Double(x, y);
	}

	public void chartMouseMoved(ChartMouseEvent arg0) {
		hif.lastEvent = arg0;

		Point2D p = getXYonChart(arg0.getTrigger());

		// stuff for editing existing annotations
		try {
			heatmapPlot.removeAnnotation(hif.xyb);
		} catch (Exception x) {
		}
		if (hif.moveArea.isSelected() && !hif.substanceEditingProcess) {
			hif.mouseOveredSubstance = null;

			for (int i = 0; i < idataset.substanceInfos.size(); i++) {
				SubstanceInfo s = idataset.substanceInfos.elementAt(i);
				if ((Math.abs(s.dt - p.getX()) < s.dtTolerance) && (Math.abs(s.rt - p.getY()) < s.rtTolerance)) {
					double left = s.dt - s.dtTolerance;
					double bottom = s.rt - s.rtTolerance;
					double right = s.dt + s.dtTolerance;
					double top = s.rt + s.rtTolerance;

					hif.xyb = new XYBoxAnnotation(left, bottom, right, top, new BasicStroke(3), Color.red);
					heatmapPlot.addAnnotation(hif.xyb);
					hif.mouseOveredSubstance = s;
				}
			}
		}

		double xViewSize = heatmapPlot.getDomainAxis().getUpperBound() - heatmapPlot.getDomainAxis().getLowerBound();

		double yViewSize = heatmapPlot.getRangeAxis().getUpperBound() - heatmapPlot.getRangeAxis().getLowerBound();

		double xtol = xViewSize / 40 * hif.visibleRectRatio;
		double ytol = yViewSize / 10 * (2 - hif.visibleRectRatio);

		if (hif.deletePeak.isSelected() || hif.addSubstance.isSelected() || hif.deleteSubstance.isSelected()
				|| hif.lichButton.isSelected()) {
			double xt = p.getX();
			double yt = p.getY();

			try {
				heatmapPlot.removeAnnotation(hif.movingtop);
				heatmapPlot.removeAnnotation(hif.movingbot);
				heatmapPlot.removeAnnotation(hif.movinglef);
				heatmapPlot.removeAnnotation(hif.movingrig);
			} catch (Exception exc) {
			}
			hif.movingtop = new XYLineAnnotation(xt - xtol, yt + ytol, xt + xtol, yt + ytol);
			hif.movingbot = new XYLineAnnotation(xt - xtol, yt - ytol, xt + xtol, yt - ytol);
			hif.movingrig = new XYLineAnnotation(xt + xtol, yt + ytol, xt + xtol, yt - ytol);
			hif.movinglef = new XYLineAnnotation(xt - xtol, yt + ytol, xt - xtol, yt - ytol);

			heatmapPlot.addAnnotation(hif.movingtop);
			heatmapPlot.addAnnotation(hif.movingbot);
			heatmapPlot.addAnnotation(hif.movinglef);
			heatmapPlot.addAnnotation(hif.movingrig);

			if (hif.substanceEditingProcess) {
				try {
					heatmapPlot.removeAnnotation(hif.movingTextannotation);
				} catch (Exception x) {
				}
				hif.movingTextannotation = new XYTextAnnotation(hif.mouseOveredSubstance.name, p.getX(), p.getY()
						+ ytol);
				hif.movingTextannotation.setFont(hif.FONT);
				hif.movingTextannotation.setTextAnchor(TextAnchor.BOTTOM_CENTER);
				heatmapPlot.addAnnotation(hif.movingTextannotation);
			}

		}

		// moving spectraplot annotation
		ChartEntity ce = arg0.getEntity();
		if (ce instanceof XYItemEntity && hif.showSpectraOnMouseover == true) {

			XYItemEntity xit = (XYItemEntity) ce;
			int itemt = xit.getItem();
			double yt = rawDataMapAligned[1][itemt];
			double xt = rawDataMapAligned[0][itemt];
			try {
				hif.spectraplot.removeAnnotation(hif.spectraDtPositionAnnotation);
				hif.spectraplot.removeAnnotation(hif.spectraPlotCoordinateText);
			} catch (Exception x) {
			}
			hif.spectraDtPositionAnnotation = new XYLineAnnotation(xt, 0, xt, 1);
			hif.spectraplot.addAnnotation(hif.spectraDtPositionAnnotation);

			DecimalFormat dt = new DecimalFormat("0.000");
			DecimalFormat rt = new DecimalFormat("#000.0");

			double yMin = hif.spectraplot.getRangeAxis().getLowerBound();
			double yMax = hif.spectraplot.getRangeAxis().getUpperBound();
			double yMean = (yMax - yMin) / 2;
			// rawDataMapAligned[2][itemt]

			hif.spectraPlotCoordinateText = new XYTextAnnotation(" " + dt.format(xt) + "/" + rt.format(yt), xt, yMean);
			hif.spectraPlotCoordinateText.setTextAnchor(TextAnchor.TOP_LEFT);
			hif.spectraplot.addAnnotation(hif.spectraPlotCoordinateText);
		}

		if (hif.showSpectraOnMouseover) {
			// ChartEntity ce = arg0.getEntity();
			if (ce instanceof XYItemEntity) {

				XYItemEntity xit = (XYItemEntity) ce;
				int itemt = xit.getItem();
				double yt = rawDataMapAligned[1][itemt];
			//	double xt = rawDataMapAligned[0][itemt];

				// Point2D p = getXYonChart(arg0.getTrigger());
				// double xt=p.getX();
				// double yt=p.getY();

				Range r = hif.heatmapPlot.getDomainAxis().getRange();
				if (hif.lastYmovedPosition != yt) {
					hif.lastYmovedPosition = yt;

					XYSeries s = new XYSeries("Spectra " + yt);
					for (int i = 0; i < hif.scaleK0.length; i++) {
						s.add(hif.scaleK0[i], 0.0);
					}

					for (int i = 0; i < rawDataMapAligned[0].length; i++) {
						if (yt == rawDataMapAligned[1][i]) {
							s.remove(rawDataMapAligned[0][i]);
							s.add(rawDataMapAligned[0][i], rawDataMapAligned[2][i]);
						}
					}
					XYSeriesCollection specdds = new XYSeriesCollection();
					specdds.addSeries(s);
					hif.spectraplot.setDataset(null);
					hif.spectraplot.setDataset(specdds);
					hif.spectraplot.getDomainAxis().setRange(r);
				}

			} else {
				hif.spectraplot.setDataset(null);
			}

		}
		// gcms moving mouseover annotation
		if (hif.showGcms) {
			double yt = p.getY();

			try {
				hif.gcmsPlot.removeAnnotation(hif.gcmsPositionAnno);
				hif.gcmsPlot.removeAnnotation(hif.gcmsPlotCoordinateText);
			} catch (Exception x) {
			}

			double xMin = hif.gcmsPlot.getDomainAxis().getLowerBound();
			double xMax = hif.gcmsPlot.getDomainAxis().getUpperBound();

			double yMin = hif.gcmsPlot.getRangeAxis().getLowerBound();
			double yMax = hif.gcmsPlot.getRangeAxis().getUpperBound();
			double yTextMargin = (yMax - yMin) / 80;

			hif.gcmsPositionAnno = new XYLineAnnotation(xMin, yt, xMax, yt);
			hif.gcmsPlot.addAnnotation(hif.gcmsPositionAnno);
			double yGcms = netCdfTools.MccToGc(yt);
			DecimalFormat df = new DecimalFormat("#.0");
			hif.gcmsPlotCoordinateText = new XYTextAnnotation(df.format(yGcms), xMax / 3, yt + yTextMargin);
			hif.gcmsPlot.addAnnotation(hif.gcmsPlotCoordinateText);
		}

		// Live Chart Modus
		if (hif.lichButton.isSelected() && hif.LiCh != null) {
			// STEP1: get peaks in range

			double xt = p.getX();
			double yt = p.getY();

			xt = Math.rint(xt * 10000) / 10000.0;
			yt = Math.rint(yt * 10) / 10.0;

			String[] substanceParameter = new String[] { "Area (" + xt + "-" + yt + ")", xt + "", yt + "", xtol + "",
					ytol + "" };
			SubstanceInfo liveSubstance = new SubstanceInfo(substanceParameter);

			// STEP2: updte the LiCh
			hif.LiCh.updateLiveChart(hif.dsif, liveSubstance);

		}
		if (hif.livePolarGlyphMenuItem.isSelected() && hif.dsif.selectedClasses != -1) {
			double xt = p.getX();
			double yt = p.getY();

			xt = Math.rint(xt * 10000) / 10000.0;
			yt = Math.rint(yt * 10) / 10.0;
			String[] substanceParameter = new String[] { "Area (" + xt + "-" + yt + ")", xt + "", yt + "", xtol + "",
					ytol + "" };
			SubstanceInfo liveSubstance = new SubstanceInfo(substanceParameter);

			try {
				heatmapPlot.removeAnnotation(hif.movingPolar);
			} catch (Exception ex) {
			}
			Image image = PolarGlyphBuilder.getLivePolarPlot(hif.dsif, liveSubstance);
			hif.movingPolar = new XYImageAnnotation(xt, yt, image);
			heatmapPlot.addAnnotation(hif.movingPolar);
		}
	}

	public void mouseClicked(MouseEvent e) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void mousePressed(MouseEvent e) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void mouseReleased(MouseEvent e) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void mouseEntered(MouseEvent e) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void mouseExited(MouseEvent e) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void mouseDragged(MouseEvent e) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void mouseMoved(MouseEvent e) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void mouseWheelMoved(MouseWheelEvent e) {
		throw new UnsupportedOperationException("Not supported yet.");
	}
}
// old code which was made for direct use in the heatmapinternal frame
// public void chartMouseMoved(ChartMouseEvent arg0) {
// lastEvent = arg0;
//
// Point2D p = getXYonChart(arg0.getTrigger());
//
//
// //stuff for editing existing annotations
// try {
// heatmapPlot.removeAnnotation(xyb);
// } catch (Exception x) {
// }
// if (moveArea.isSelected() && !substanceEditingProcess) {
// mouseOveredSubstance = null;
//
// for (int i = 0; i < idataset.substanceInfos.size(); i++) {
// SubstanceInfo s = idataset.substanceInfos.elementAt(i);
// if ((Math.abs(s.dt - p.getX()) < s.dtTolerance) && (Math.abs(s.rt - p.getY()) < s.rtTolerance)) {
// double left = s.dt - s.dtTolerance;
// double bottom = s.rt - s.rtTolerance;
// double right = s.dt + s.dtTolerance;
// double top = s.rt + s.rtTolerance;
//
// xyb = new XYBoxAnnotation(left, bottom, right, top, new BasicStroke(3), Color.red);
// heatmapPlot.addAnnotation(xyb);
// mouseOveredSubstance = s;
// }
// }
// }
//
//
//
//
// double xViewSize =
// heatmapPlot.getDomainAxis().getUpperBound()
// - heatmapPlot.getDomainAxis().getLowerBound();
//
// double yViewSize =
// heatmapPlot.getRangeAxis().getUpperBound()
// - heatmapPlot.getRangeAxis().getLowerBound();
//
// double xtol = xViewSize / 40 * visibleRectRatio;
// double ytol = yViewSize / 10 * (2 - visibleRectRatio);
//
// if (deletePeak.isSelected() || addSubstance.isSelected() || deleteSubstance.isSelected() || lichButton.isSelected())
// {
// double xt = p.getX();
// double yt = p.getY();
//
// try {
// heatmapPlot.removeAnnotation(movingtop);
// heatmapPlot.removeAnnotation(movingbot);
// heatmapPlot.removeAnnotation(movinglef);
// heatmapPlot.removeAnnotation(movingrig);
// } catch (Exception exc) {
// }
// movingtop = new XYLineAnnotation(xt - xtol, yt + ytol, xt + xtol, yt + ytol);
// movingbot = new XYLineAnnotation(xt - xtol, yt - ytol, xt + xtol, yt - ytol);
// movingrig = new XYLineAnnotation(xt + xtol, yt + ytol, xt + xtol, yt - ytol);
// movinglef = new XYLineAnnotation(xt - xtol, yt + ytol, xt - xtol, yt - ytol);
//
// heatmapPlot.addAnnotation(movingtop);
// heatmapPlot.addAnnotation(movingbot);
// heatmapPlot.addAnnotation(movinglef);
// heatmapPlot.addAnnotation(movingrig);
//
// if (substanceEditingProcess) {
// try {
// heatmapPlot.removeAnnotation(movingTextannotation);
// } catch (Exception x) {
// }
// movingTextannotation = new XYTextAnnotation(mouseOveredSubstance.name, p.getX(), p.getY() + ytol);
// movingTextannotation.setFont(FONT);
// movingTextannotation.setTextAnchor(TextAnchor.BOTTOM_CENTER);
// heatmapPlot.addAnnotation(movingTextannotation);
// }
//
//
// }
//
// //moving spectraplot annotation
// ChartEntity ce = arg0.getEntity();
// if (ce instanceof XYItemEntity && showSpectraOnMouseover == true) {
//
// XYItemEntity xit = (XYItemEntity) ce;
// int itemt = xit.getItem();
// double yt = rawDataMapAligned[1][itemt];
// double xt = rawDataMapAligned[0][itemt];
// try {
// spectraplot.removeAnnotation(spectraDtPositionAnnotation);
// spectraplot.removeAnnotation(spectraPlotCoordinateText);
// } catch (Exception x) {
// }
// spectraDtPositionAnnotation = new XYLineAnnotation(xt, 0, xt, 1);
// spectraplot.addAnnotation(spectraDtPositionAnnotation);
//
// DecimalFormat dt = new DecimalFormat("0.000");
// DecimalFormat rt = new DecimalFormat("#000.0");
//
// double yMin = spectraplot.getRangeAxis().getLowerBound();
// double yMax = spectraplot.getRangeAxis().getUpperBound();
// double yMean = (yMax - yMin) / 2;
// //rawDataMapAligned[2][itemt]
//
// spectraPlotCoordinateText = new XYTextAnnotation(" " + dt.format(xt) + "/" + rt.format(yt), xt, yMean);
// spectraPlotCoordinateText.setTextAnchor(TextAnchor.TOP_LEFT);
// spectraplot.addAnnotation(spectraPlotCoordinateText);
// }
//
//
//
// if (showSpectraOnMouseover) {
// //ChartEntity ce = arg0.getEntity();
// if (ce instanceof XYItemEntity) {
//
// XYItemEntity xit = (XYItemEntity) ce;
// int itemt = xit.getItem();
// double yt = rawDataMapAligned[1][itemt];
// double xt = rawDataMapAligned[0][itemt];
//
// // Point2D p = getXYonChart(arg0.getTrigger());
// // double xt=p.getX();
// // double yt=p.getY();
//
// if (lastYmovedPosition != yt) {
// lastYmovedPosition = yt;
//
// XYSeries s = new XYSeries("Spectra " + yt);
// for (int i = 0; i < scaleK0.length; i++) {
// s.add(scaleK0[i], 0.0);
// }
//
// for (int i = 0; i < rawDataMapAligned[0].length; i++) {
// if (yt == rawDataMapAligned[1][i]) {
// s.remove(rawDataMapAligned[0][i]);
// s.add(rawDataMapAligned[0][i], rawDataMapAligned[2][i]);
// }
// }
// XYSeriesCollection specdds = new XYSeriesCollection();
// specdds.addSeries(s);
// spectraplot.setDataset(null);
// spectraplot.setDataset(specdds);
// }
//
//
//
// } else {
// spectraplot.setDataset(null);
// }
//
//
// }
// //gcms moving mouseover annotation
// if (showGcms) {
// double yt = p.getY();
//
// try {
// gcmsPlot.removeAnnotation(gcmsPositionAnno);
// gcmsPlot.removeAnnotation(gcmsPlotCoordinateText);
// } catch (Exception x) {
// }
//
// double xMin = gcmsPlot.getDomainAxis().getLowerBound();
// double xMax = gcmsPlot.getDomainAxis().getUpperBound();
//
// double yMin = gcmsPlot.getRangeAxis().getLowerBound();
// double yMax = gcmsPlot.getRangeAxis().getUpperBound();
// double yTextMargin = (yMax - yMin) / 80;
//
// gcmsPositionAnno = new XYLineAnnotation(xMin, yt, xMax, yt);
// gcmsPlot.addAnnotation(gcmsPositionAnno);
// double yGcms = netCdfTools.MccToGc(yt);
// DecimalFormat df = new DecimalFormat("#.0");
// gcmsPlotCoordinateText = new XYTextAnnotation(df.format(yGcms), xMax / 3, yt + yTextMargin);
// gcmsPlot.addAnnotation(gcmsPlotCoordinateText);
// }
//
//
// //Live Chart Modus
// if (lichButton.isSelected() && LiCh != null) {
// //STEP1: get peaks in range
//
// double xt = p.getX();
// double yt = p.getY();
//
// xt = Math.rint(xt * 10000) / 10000.0;
// yt = Math.rint(yt * 10) / 10.0;
//
// String[] substanceParameter = new String[]{
// "Area (" + xt + "-" + yt + ")",
// xt + "",
// yt + "",
// xtol + "",
// ytol + ""
// };
// SubstanceInfo liveSubstance = new SubstanceInfo(substanceParameter);
//
// //STEP2: updte the LiCh
// LiCh.updateLiveChart(dsif, liveSubstance);
//
//
//
//
//
// }
// if (livePolarGlyphMenuItem.isSelected() && dsif.selectedClasses != -1) {
// double xt = p.getX();
// double yt = p.getY();
//
// xt = Math.rint(xt * 10000) / 10000.0;
// yt = Math.rint(yt * 10) / 10.0;
// String[] substanceParameter = new String[]{
// "Area (" + xt + "-" + yt + ")",
// xt + "",
// yt + "",
// xtol + "",
// ytol + ""
// };
// SubstanceInfo liveSubstance = new SubstanceInfo(substanceParameter);
//
// try {
// heatmapPlot.removeAnnotation(movingPolar);
// } catch (Exception ex) {
// }
// Image image = PolarGlyphBuilder.getLivePolarPlot(dsif, liveSubstance);
// movingPolar = new XYImageAnnotation(xt, yt, image);
// heatmapPlot.addAnnotation(movingPolar);
// }
// }
//
// public void chartMouseClicked(ChartMouseEvent arg0) {
// MouseEvent trigger = arg0.getTrigger();
// ChartEntity ce = arg0.getEntity();
// Point2D p = getXYonChart(arg0.getTrigger());
//
// if (ce instanceof LegendItemEntity) {
// LegendItemEntity l = (LegendItemEntity) ce;
// l.getSeriesKey();
// XYSeriesCollection col = ((XYSeriesCollection) spectraplot.getDataset());
// XYSeries s = col.getSeries(l.getSeriesKey());
// JPopupMenu pop = new legendItemEditorPopup(col, s);
// pop.show(cp, arg0.getTrigger().getX(), arg0.getTrigger().getY());
// }
//
// double xViewSize =
// heatmapPlot.getDomainAxis().getUpperBound()
// - heatmapPlot.getDomainAxis().getLowerBound();
//
// double yViewSize =
// heatmapPlot.getRangeAxis().getUpperBound()
// - heatmapPlot.getRangeAxis().getLowerBound();
//
// double xtol = xViewSize / 40 * visibleRectRatio;
// double ytol = yViewSize / 10 * (2 - visibleRectRatio);
//
//
// if (addPeak.isSelected() && ce instanceof XYItemEntity) {
//
// if (idataset.peaklists.containsKey(measurement.measurementID) == false) {
// String filename = new File(measurement.path).getName().replace("ims.csv", "iphex.pl");
// String[] header = new String[]{"intensity_max/ripmax" + "1/k0" + "RT" + "1/k0_radius" + "RT_radius"};
// Peaklist pl = new Peaklist(header, measurement.measurementID, filename);
// idataset.peaklists.put(measurement.measurementID, pl);
// }
//
// XYItemEntity xi = (XYItemEntity) ce;
// int item = xi.getItem();
//
// double x = Math.rint(rawDataMapAligned[0][item] * 1000) / 1000.0;
// double y = Math.rint(rawDataMapAligned[1][item] * 10) / 10.0;
// double intensity = Math.rint(rawDataMapAligned[2][item] * 10000) / 10000.0;
//
// //y = y/measurement.align ? ;
//
// Peak klickedPeak = new Peak(new String[]{intensity + "", x + "", y + "", 0.01 + "", 8 + ""});
// idataset.peaklists.get(measurement.measurementID).getPeaks().add(klickedPeak);
// dsif.updateTableWithoutHeatmap();
// showPeaklist();
// }
//
// if (deletePeak.isSelected()) {
//
// double xt = p.getX();
// double yt = p.getY();
//
// if (idataset.peaklists.containsKey(measurement.measurementID)) {
// Peaklist peaklist = idataset.peaklists.get(measurement.measurementID);
// for (int i = 0; i < peaklist.getPeaks().size(); i++) {
// if (peaklist.getPeaks().get(i).rt >= yt - ytol && peaklist.getPeaks().get(i).rt <= yt + ytol) {
// if (peaklist.getPeaks().get(i).dt >= xt - xtol && peaklist.getPeaks().get(i).dt <= xt + xtol) {
// peaklist.getPeaks().remove(i);
// i--;
// }
// }
// }
// showPeaklist();
// dsif.updateTableWithoutHeatmap();
// }
// }
//
// if (addSubstance.isSelected()) {
// double xt = p.getX();
// double yt = p.getY();
//
// xt = Math.rint(xt * 10000) / 10000.0;
// yt = Math.rint(yt * 10) / 10.0;
//
// String substanceName = "Area (" + xt + "-" + yt + ")";
//
// if (substanceEditingProcess) {
//
// substanceName = mouseOveredSubstance.name;
//
// }
//
// String[] substanceParameter = new String[]{
// substanceName,
// xt + "",
// yt + "",
// Math.rint(xtol * 10000) / 10000.0 + "",
// Math.rint(ytol * 10) / 10.0 + ""
// };
// SubstanceInfo si = new SubstanceInfo(substanceParameter);
// idataset.addSubstanceInfo(si);
// dsif.updateTableWithoutHeatmap();
//
// if (substanceEditingProcess) {
// substanceEditingProcess = false;
// mouseOveredSubstance = null;
// moveArea.setSelected(true);
//
// }
//
// showSubstanceInfos();
// }
// if (mouseOveredSubstance != null) {
// idataset.substanceInfos.remove(mouseOveredSubstance);
// showSubstanceInfos();
// substanceEditingProcess = true;
// if (mouseOveredSubstance != null) {
// addSubstance.setSelected(true);
// }
// }
//
//
// if (deleteSubstance.isSelected()) {
// double xt = p.getX();
// double yt = p.getY();
//
// xt = Math.rint(xt * 10000) / 10000.0;
// yt = Math.rint(yt * 10) / 10.0;
//
// boolean areaDeleted = false;
// for (int i = 0; i < idataset.substanceInfos.size(); i++) {
// if (idataset.substanceInfos.elementAt(i).rt >= yt - ytol && idataset.substanceInfos.elementAt(i).rt <= yt + ytol) {
// if (idataset.substanceInfos.elementAt(i).dt >= xt - xtol && idataset.substanceInfos.elementAt(i).dt <= xt + xtol) {
// idataset.substanceInfos.remove(idataset.substanceInfos.elementAt(i));
// i--;
// areaDeleted = true;
// }
// }
// if (areaDeleted) {
// dsif.updateTableWithoutHeatmap();
// showSubstanceInfos();
// areaDeleted = false;
// }
// }
// }
//
// if (trigger.isControlDown() && trigger.getButton() == 1 && ce instanceof XYItemEntity && showGcms == false) {
// showSpectraOnMouseover = false;
//
// XYItemEntity xit = (XYItemEntity) ce;
// int itemt = xit.getItem();
// double yt = rawDataMapAligned[1][itemt];
// //double xt = rawDataMapAligned[0][itemt];
//
// XYSeries s = new XYSeries("Spectra " + yt);
// s.setDescription(measurement.filename.replace("_ims.csv", ""));
// for (int i = 0; i < scaleK0.length; i++) {
// s.add(scaleK0[i], 0.0);
// }
//
// for (int i = 0; i < rawDataMapAligned[0].length; i++) {
// if (yt == rawDataMapAligned[1][i]) {
// s.remove(rawDataMapAligned[0][i]);
// s.add(rawDataMapAligned[0][i], rawDataMapAligned[2][i]);
// }
// }
// try {
// klickedpectraDds.getSeries(s.getKey());
// } catch (Exception x) {
// klickedpectraDds.addSeries(s);
// XYLineAnnotation xyla = new XYLineAnnotation(-1, yt, 3, yt);
// heatmapPlot.addAnnotation(xyla);
// klickedSpectraAnnotations.add(xyla);
// }
// spectraplot.setDataset(klickedpectraDds);
//
//
// try {
// XYLineAnnotation toAdd = (XYLineAnnotation) spectraDtPositionAnnotation.clone();
// spectraplot.addAnnotation(toAdd);
// XYTextAnnotation toAdd2 = (XYTextAnnotation) spectraPlotCoordinateText.clone();
// spectraplot.addAnnotation(toAdd2);
// } catch (CloneNotSupportedException ex) {
// }
//
//
// }
//
// if (trigger.isControlDown() && trigger.getButton() == 1 && showGcms == true) {
//
// double yt = p.getY();
//
// double xMin = gcmsPlot.getDomainAxis().getLowerBound();
// double xMax = gcmsPlot.getDomainAxis().getUpperBound();
//
// double yMin = gcmsPlot.getRangeAxis().getLowerBound();
// double yMax = gcmsPlot.getRangeAxis().getUpperBound();
// double yTextMargin = (yMax - yMin) / 80;
//
// gcmsPositionAnno = new XYLineAnnotation(xMin, yt, xMax, yt);
// gcmsPlot.addAnnotation(gcmsPositionAnno);
// double yGcms = netCdfTools.MccToGc(yt);
// DecimalFormat df = new DecimalFormat("#.0");
// gcmsPlotCoordinateText = new XYTextAnnotation(df.format(yGcms), xMax / 3, yt + yTextMargin);
// gcmsPlot.addAnnotation(gcmsPlotCoordinateText);
//
// klickedGcmsAnnotations.add(gcmsPositionAnno);
// klickedGcmsTextAnnotations.add(gcmsPlotCoordinateText);
//
// XYLineAnnotation xyla = new XYLineAnnotation(-1, yt, 3, yt);
// heatmapPlot.addAnnotation(xyla);
// klickedSpectraAnnotations.add(xyla);
//
//
// }
//
// //set spectraplot axis to autoscale an remove all marked annotations
// if (trigger.isControlDown() && trigger.getClickCount() == 2) {
// showSpectraOnMouseover = true;
// klickedpectraDds.removeAllSeries();
// for (Object xya : spectraplot.getAnnotations()) {
// spectraplot.removeAnnotation((XYAnnotation) xya);
// }
// for (XYLineAnnotation xyla : klickedSpectraAnnotations) {
// try {
// heatmapPlot.removeAnnotation(xyla);
// } catch (Exception x) {
// }
// }
// for (XYLineAnnotation xyla : klickedGcmsAnnotations) {
// try {
// gcmsPlot.removeAnnotation(xyla);
// } catch (Exception x) {
// }
// }
// for (XYTextAnnotation xyta : klickedGcmsTextAnnotations) {
// try {
// gcmsPlot.removeAnnotation(xyta);
// } catch (Exception x) {
// }
// }
// }
//
// //klicking the mouswheel aktivates autosclae on subplots
// if (trigger.getButton() == 2) {
// if (trigger.isShiftDown() || trigger.isControlDown()) {
// if (showGcms) {
// gcmsPlot.getDomainAxis().setAutoRange(true);
// } else {
// spectraplot.getRangeAxis().setAutoRange(true);
// }
// }
// }
//
// showSubstanceInfos();
// }
